package com.soma.game.job;


import com.soma.game.model.SomaUser;
import com.soma.game.service.GameScoreService;
import com.soma.game.service.UserService;
import com.soma.game.service.soma.SomaService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: soma-game
 * @description: 清除阶段得分数据
 * @author: hduprince
 * @create: 2018-07-18 16:40
 **/
@Component
public class RefushUserJob {

    Logger logger = LoggerFactory.getLogger(RefushUserJob.class);



    @Resource
    private UserService userService;

    @Resource
    private GameScoreService gameScoreService;

    @Resource
    private SomaService somaService;

    @Scheduled(cron = "0 0 2 * * *")
    //@Scheduled(fixedDelay = 1000)
    public void execute(){
        logger.info("start : 刷新用户头像昵称");
        int pageSize = 10;
        int page = 1;
        List<Long> uids = gameScoreService.getYesterdayUsers();

        for(;page<uids.size()/pageSize+2;page++){
            List<Long> batch = uids.stream().skip(pageSize*(page-1)).limit(pageSize).collect(Collectors.toList());
            String uidString = StringUtils.join(batch, ",");
            List<SomaUser> somaUsers = somaService.getUsers(uidString);

            for (SomaUser somaUser : somaUsers){
                userService.updateBySomaUser(somaUser);
            }

        }

        logger.info("end : 刷新用户头像昵称");
    }
}
